package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    /*@Override
    public List<ItemCat> findItemCatList(Integer level) {
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        for (ItemCat oneitemCat:oneList) {
            queryWrapper.clear();
            queryWrapper.eq("parent_id", oneitemCat.getId());
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for (ItemCat twoitemCat :twoList) {
                queryWrapper.clear();
                queryWrapper.eq("parent_id", twoitemCat.getId());
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                twoitemCat.setChildren(threeList);
            }
            oneitemCat.setChildren(twoList);
        }
        return oneList;
    }
*/
    /**
     * 1.数据结构:  Map<K,V>  key=parentId   value="List<ItemCat>"
     * 2.封装Map的数据类型
     * 3.如果level=1 只获取一级.
     * 4.如果level=2 获取一级,一级嵌套二级
     * 5.如果level=3 获取一级,一级嵌套二级,二级嵌套三级.
     * @param level
     * @return
     */
    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        //1.封装Map集合
        Map<Integer,List<ItemCat>> map = getMap();
        //2.判断level的值
        if(level == 1){
            return map.get(0);
        }

        if(level ==2){
            return  getTwoList(map);
        }

        //如果level不是1-2级则一定是三级
        List<ItemCat> list = getThreeList(map);
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime - startTime)+"毫秒");
        return list;
    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        //获取一级和二级
        List<ItemCat> oneList = getTwoList(map);
        //封装三级，遍历二级菜单，之后封装
        for (ItemCat oneItemCat:oneList){
            //获取二级集合
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList == null || twoList.size()==0){
                continue;
            }
            for(ItemCat twoItemCat:twoList){
                int parentId = twoItemCat.getId();
                List<ItemCat> threeList = map.get(parentId);
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        //1、先获取一级列表
        List<ItemCat> oneList = map.get(0);
        //2、根据一级查询二级
        for (ItemCat oneItemCat:
             oneList) {
            //查询二级，所在parentId是一级的Id
            int parentId = oneItemCat.getId();
            List<ItemCat> twoList = map.get(parentId);
            //封装数据
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }

    /**
     * 1.查询所有的商品分类列表.   查询一次数据库.
     * 2.循环遍历所有的数据,按照parentId,List<ItemCat>方式封装数据.
     * 3.????
     * @return
     */
    private Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        List<ItemCat> list = itemCatMapper.selectList(null);
        for (ItemCat itemCat:
             list) {
            //获取parentId
            int parentId = itemCat.getParentId();
            if (map.containsKey(parentId)){
                //key存在
                map.get(parentId).add(itemCat);
            }else {
                //key不存在
                List<ItemCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                //将第一个元素封装到map中
                map.put(parentId,childrenList);
            }
        }
        return map;

    }


    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    public void addItemCat(ItemCat itemCat) {

        itemCatMapper.insert(itemCat.setStatus(true));

    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        int level = itemCat.getLevel();
        if(level==3){
            itemCatMapper.deleteById(itemCat.getId());
        }
        if (level ==2){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",itemCat.getId())
                        .or()
                        .eq("id",itemCat.getId());
            itemCatMapper.delete(queryWrapper);
        }
        if(level==1){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", itemCat.getId());
            List<Object> twoIdList = itemCatMapper.selectObjs(queryWrapper);//selectObjs获取第一列的信息，一般第一列都是id

            queryWrapper.clear();
            queryWrapper.in(twoIdList.size()>0,"parent_id", twoIdList)
                        .or()
                        .eq("parent_id", itemCat.getId())
                        .or()
                        .eq("id", itemCat.getId());
            itemCatMapper.delete(queryWrapper);
        }
    }
}
